package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsMCulture;
import com.wenwu.common.bean.RelicsMCultureExample;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsMCultureMapper;
import com.wenwu.service.MoveableCultureService;

@Service
public class MoveableCultureServiceImpl implements MoveableCultureService {

	@Autowired
	private RelicsMCultureMapper relicsMCultureMapper;
	
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	
	public RelicsMCulture selectMCultureByMWenwuKey(Integer key)
			throws RelicsException {
		try {
			RelicsMCultureExample example = new RelicsMCultureExample();
			example.createCriteria().andMRelicsIdEqualTo(key);
			List<RelicsMCulture> list = relicsMCultureMapper.selectByExample(example);
			if(list == null || list.size() <=0){
				return null;
			}else{
				return list.get(0);
			}
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public String addRelicsMCuture(RelicsMCulture relicsMculutre)
			throws RelicsException {
		try{
			String result = null;
			int num = 0 ;
			List<RelicsAttachment> list = relicsMculutre.getFileList();
			if (relicsMculutre.getmCultureId() == null) {
				num += relicsMCultureMapper.insertSelective(relicsMculutre);
				if (relicsMculutre.getmCultureId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("可移动文物-文物面貌附件");
							attachment.setTableOwner(relicsMculutre.getmCultureId());// 外键
						}
						num += attachmentMapper.insertList(list);
					}
				}
				if (num > 0) {
					result = "可移动文物-文物面貌添加成功";
				} else {
					result = "可移动文物-文物面貌添加失败";
				}
			} else {
				num += attachmentMapper.deleteByKey(relicsMculutre.getmCultureId(),relicsMculutre.getOwnList());//先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("可移动文物-文物面貌附件");
						attachment.setTableOwner(relicsMculutre.getmCultureId());// 外键
					}
					num += attachmentMapper.insertList(list);
				}
				num += relicsMCultureMapper.updateByPrimaryKeySelective(relicsMculutre);
				if (num > 0) {
					result = "可移动文物-文物面貌修改成功";
				} else {
					result = "可移动文物-文物面貌修改失败";
				}
			}
			return result;
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
		
	}

	public PaginationVO selectByCondition(RelicsMCulture relicsMCulture,
			PaginationVO pageVo) throws RelicsException{
		try {
			List<RelicsMCulture> list = relicsMCultureMapper.selectListByPage(relicsMCulture, pageVo);
			int count = relicsMCultureMapper.selectTotalRecord(relicsMCulture);
			pageVo.setList(list);
			pageVo.setTotalRecord(count);
			return pageVo;
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public RelicsMCulture selectMCultureBySelftKey(Integer key)
			throws RelicsException {
		try {
			return relicsMCultureMapper.selectByPrimaryKey(key);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsMCultureMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}

}
